package com.newlink.building.views

import android.app.Dialog
import android.content.Context
import android.os.Bundle
import android.view.Gravity
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.luck.picture.lib.decoration.GridSpacingItemDecoration
import com.newlink.building.common_base.utils.NLog
import com.newlink.building.R
import com.newlink.building.activities.call.viewmodels.CallViewModel
import com.newlink.building.adapter.OptionItem
import com.newlink.building.adapter.SingleChoiceAdapter
import com.newlink.building.utils.Module_Phone_Event

/**
 * @author Jake
 * @date 2022/10/11
 * @description AudioRoute 选择
 */
class Module_Phone_AudioRouteSelectDialog(context: Context, val viewModel: CallViewModel) :
    Dialog(context, R.style.bg_audio_route_dialog) {


    init {

        window?.let {
            val params = it.attributes
            val displayMetrics = context.resources.displayMetrics
            val width = (displayMetrics.widthPixels * 0.9).toInt()
            params.gravity = Gravity.CENTER
            params.width = width
            params.verticalMargin = -0.05f
            it.attributes = params
            it.setBackgroundDrawableResource(android.R.color.transparent)
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.dialog_recycler_view)

        initAudioRouteContent()
    }

    private fun initAudioRouteContent() {
        val recyclerView: RecyclerView = findViewById(R.id.recycler_view)

        val options = viewModel.getSupportAudioRouteList()
        val currentAudioDevice = viewModel.getCurrentAudioRoute()
        recyclerView.layoutManager = GridLayoutManager(context, 2)
        recyclerView.addItemDecoration(GridSpacingItemDecoration(2, 20, false))
        recyclerView.adapter = SingleChoiceAdapter(options, currentAudioDevice) { selectedOption ->
            NLog.e("[AudioRouteSelectDialog] ... $selectedOption")
            onOptionSelected(selectedOption)
            dismissDialog()
        }
    }

    private fun onOptionSelected(selectedOption: OptionItem) {
        if (selectedOption.type == viewModel.getCurrentAudioRoute()) {
            return
        }
        viewModel.audioRouteChanged.value = Module_Phone_Event(true)
        viewModel.switchAudioRoute(selectedOption.type)
    }

    override fun show() {
        super.show()
        window?.let {
            val params = it.attributes
            val displayMetrics = context.resources.displayMetrics
//            val width = (displayMetrics.widthPixels * 0.9).toInt()
            params.gravity = Gravity.CENTER
//            params.width = width
            params.verticalMargin = -0.1f
            it.attributes = params
            it.setBackgroundDrawableResource(android.R.color.transparent)
        }
    }

    private fun dismissDialog() {
        viewModel.showRouteDialog.value = false
        dismiss()

//        MainScope().launch {
//            delay(80)
//            withContext(Dispatchers.Main) {
//                viewModel.audioRouteChanged.value = Event(true)
//            }
//
//        }

    }
}
